from ftplib import FTP
import os

# FTP服务器信息
ftp_server = 'localhost'
# ftp_server = '8.137.57.138'
ftp_port = 2121  # 使用你在FTP服务器中指定的端口
ftp_user = 'user'
ftp_password = '12345'


# 上传图片到FTP服务器
def upload_image_to_ftp(local_path, remote_path):
    try:
        ftp = FTP()#创建一个 FTP 对象。
        ftp.connect(ftp_server, ftp_port)#连接到指定的 FTP 服务器和端口。
        ftp.login(ftp_user, ftp_password)#使用提供的用户名和密码登录到 FTP 服务器。
        # 以二进制读模式打开本地文件,将文件上传到 FTP 服务器。STOR 是 FTP 命令，用于存储文件，remote_path 是文件在服务器上的路径。
        with open(local_path, 'rb') as file:
            ftp.storbinary(f'STOR {remote_path}', file)

        ftp.quit()
        print(f"Successfully uploaded {local_path} to {ftp_server}")
    except Exception as e:
        print(f"Failed to upload {local_path} to {ftp_server}: {e}")

# 本地图片文件路径
remote_image_path = ['horse.jpg',"relaxing-cat.jpg","xiaoren.gif"]
# local_image_path = 'upload/'+remote_image_path



# 调用上传函数
for i in remote_image_path:
    local_image_path = 'upload/' +i
    upload_image_to_ftp(local_image_path, i)
